﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace jQuery.Validation.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public sealed class RangeAttribute : ValidationAttribute
    {
        public int Minimum { get; set; }
        public int Maximum { get; set; }

        public RangeAttribute(int minimum, int maximum)
        {
            Minimum = minimum;
            Maximum = maximum;
        }

        public override bool IsValid(object value)
        {
            if (value == null)
                return true;

            double doubleValue;
            if(double.TryParse(value.ToString(), out doubleValue))
            {
                if (doubleValue >= Minimum && doubleValue <= Maximum)
                    return true;

                return false;
            }
            return true;
            
        }
    }
}
